package com.lvy.hczn.transmit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.lvy.hczn.front.common.constant.Constants;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.PackSpec;
import com.lvy.hczn.front.system.service.PackSpecService;
import com.lvy.hczn.mq.rocket.ParamInfo;
import com.lvy.hczn.mq.rocket.ProducerUtil;
import com.lvy.hczn.transmit.domain.dto.cteck.PackSpecDto;
import com.lvy.hczn.transmit.service.TPackSpecService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Service
@Slf4j
public class TPackSpecServiceImpl implements TPackSpecService {

    @Autowired
    private ProducerUtil producerUtil;

    @Autowired
    private PackSpecService packSpecService;

    /**
     * 平台推送包装袋规格信息
     *
     * @param request
     * @param info
     */
    @Override
    public void syncPackSpecFromPlatForm(HttpServletRequest request, ParamInfo info) {
        log.error("syncPackSpecFromPlatForm:{}", JSON.toJSONString(info));
        if (info != null && info.getData() != null) {
            PackSpecDto packSpecDto = JSON.parseObject(JSON.toJSONString(info.getData()), PackSpecDto.class);
            if (StrUtil.isEmpty(packSpecDto.getId())) {
                throw new UtilException("ID为空");
            }
            if (StrUtil.isEmpty(packSpecDto.getSpuCode())) {
                throw new UtilException("商品编码为空");
            }
            if (StrUtil.isEmpty(packSpecDto.getPackName())) {
                throw new UtilException("包装袋名称为空");
            }
            if (packSpecDto.getQuantity() == null) {
                throw new UtilException("包装份数为空");
            }
            if (packSpecDto.getPackFee() == null) {
                throw new UtilException("加工打包费用为空");
            }
            if (packSpecDto.getVolume() == null || packSpecDto.getVolume().compareTo(BigDecimal.ZERO) < 1) {
                throw new UtilException("包装袋装载体积为空或为0");
            }
            PackSpec packSpec = new PackSpec();
            BeanUtil.copyProperties(packSpecDto, packSpec, false);
            packSpec.setSourceId(packSpecDto.getId() + Constants.SOURCE_ID_DEFAULT);
            packSpec.setId(null);
            packSpec.setDelFlag(packSpecDto.getStatus());
            packSpecService.saveOrUpdateInfo(packSpec, true);
        } else {
            log.error("syncPackSpecFromPlatFormError:{}", JSON.toJSONString(info));
            throw new UtilException("数据信息为空");
        }
    }
}
